﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;   //호환되지 않은 Dll을 사용할때
using Microsoft.Win32;      // Load Registry
using System.Diagnostics;   // 시스템 프로세스, 이벤트 로그 및 성능 카운터와 상호작용 할 수 있는 클래스를 가진 네임 스페이스

namespace NH70_Debugger
{
    public partial class RegEdit : Form
    {
        [StructLayout(LayoutKind.Sequential)]
        internal struct SYSTEM_INFO
        {
            public ushort wProcessorArchitecture;
            public ushort wReserved;
            public uint dwPageSize;
            public IntPtr lpMinimumApplicationAddress;
            public IntPtr lpMaximumApplicationAddress;
            public UIntPtr dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public ushort wProcessorLevel;
            public ushort wProcessorRevision;

        }

        [DllImport("kernel32.dll")]
        //GetNativeSystemInfo함수는 SYSTEM_INFO구조체의 첫번째 wProcessorArchitecture에 32/64비트(bit) 체계정보를 담게 됩니다.
        internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSysteminfo);


        public RegEdit()
        {
            InitializeComponent();

            InitDisplay();
        }

        private void InitDisplay()
        {
            string RegSub = @"";

            SYSTEM_INFO sysInfo = new SYSTEM_INFO();
            GetNativeSystemInfo(ref sysInfo);

            //sysinfo의 wProcessorArchitecture값이 0이면 32bit, 6이면 IA계열의 64bit, 9이면 일반적인 64bit
            if (sysInfo.wProcessorArchitecture == 6 || sysInfo.wProcessorArchitecture == 9)
                RegSub = @"\Wow6432Node";

            string RegRoot = @"SOFTWARE"+RegSub+@"\ODBC\ODBC.INI\NH70DBCON";;

            RegistryKey reg = Registry.LocalMachine;

            reg = reg.OpenSubKey(RegRoot, true);

            if( reg != null ){

                string sVal = Convert.ToString(reg.GetValue("Database"));
                textBox_Database.Text = sVal.Trim();

                sVal = Convert.ToString(reg.GetValue("Driver"));
                textBox_Driver.Text = sVal.Trim();

                sVal = Convert.ToString(reg.GetValue("LastUser"));
                textBox_LastUser.Text = sVal.Trim();

                sVal = Convert.ToString(reg.GetValue("Server"));
                textBox_Server.Text = sVal.Trim();

                reg.Close();
            }            
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_Apply_Click(object sender, EventArgs e)
        {
           string RegSub = @"";

            SYSTEM_INFO sysInfo = new SYSTEM_INFO();
            GetNativeSystemInfo(ref sysInfo);

            //sysinfo의 wProcessorArchitecture값이 0이면 32bit, 6이면 IA계열의 64bit, 9이면 일반적인 64bit
            if (sysInfo.wProcessorArchitecture == 6 || sysInfo.wProcessorArchitecture == 9)
                RegSub = @"\Wow6432Node";

            string RegRoot = @"SOFTWARE"+RegSub+@"\ODBC\ODBC.INI\NH70DBCON";;

            RegistryKey reg = Registry.LocalMachine;

            reg = reg.OpenSubKey(RegRoot, true);

            if (reg != null)
            {
                if (textBox_Database.Text.Trim().Length > 0 )
                    reg.SetValue("Database", textBox_Database.Text.Trim());

                if (textBox_Driver.Text.Trim().Length > 0)
                    reg.SetValue("Driver", textBox_Driver.Text.Trim());

                if (textBox_LastUser.Text.Trim().Length > 0)
                    reg.SetValue("LastUser", textBox_LastUser.Text.Trim());

                if (textBox_Server.Text.Trim().Length > 0)
                    reg.SetValue("Server", textBox_Server.Text.Trim());

                reg.Close();
            }

            InitDisplay();
        }

        private void button_odbc_Click(object sender, EventArgs e)
        {

            SYSTEM_INFO sysInfo = new SYSTEM_INFO();
            GetNativeSystemInfo(ref sysInfo);

            string sysSub = @"";            

            //sysinfo의 wProcessorArchitecture값이 0이면 32bit, 6이면 IA계열의 64bit, 9이면 일반적인 64bit
            if (sysInfo.wProcessorArchitecture == 6 || sysInfo.wProcessorArchitecture == 9)
                sysSub = @"\sysWow64";

            string runPath = Environment.GetEnvironmentVariable("windir") +sysSub+ @"\odbcad32.exe";

            Process.Start(runPath);
        }
    }
}